import { BaseWindow } from "../../../../component/window/BaseWindow";
import { PageTool } from "../../../../common/global/PageTool";
import { EventName } from "../../../mvc/EventName";
import { ObjCreate } from "../../common/ObjCreate";
import { Language } from "../../../../../extend/language/Language";
import { TilePanel } from "../../equipment/TilePanel";
import { ReplyRenderer } from "./ReplyRenderer";
import { PageSelecter } from "../../common/pageSelect/PageSelecter";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { DataEvent } from "../../../events/DataEvent";
import { SGuildWarehouseApply } from "../../../../../Message/Game/SGuildWarehouseApply";
import { SPlayerItem } from "../../../../../Message/Public/SPlayerItem";
import { ItemData } from "../../../resource/info/ItemData";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
//class ReplyListWindow
    
    export  class ReplyListWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this._pageTool = new PageTool([], 5);
            this.init();
            this.addEventListener(flash.events.Event.CHANGE, this.onCurPageChangeHandler);
            this.addEventListener(EventName.OperateRequestInfo, this.onOperateRequestInfoHandler);
            return;
        }

        public init(): void
        {
            this.setSize(275, 342);
            this.titleHeight = 28;
            ObjCreate.createBg(17, 40, 240, 290, this);
            this.title = Language.getString(57271);
            this._itemListPanel = new TilePanel();
            this._itemListPanel.horizontalGap = 3;
            this._itemListPanel.verticalGap = 2;
            this._itemListPanel.setSize(245, 265);
            this._itemListPanel.columnWidth = 245;
            this._itemListPanel.rowHeight = 50;
            this._itemListPanel.setStyle("cellRenderer", ReplyRenderer);
            this._itemListPanel.move(16, 40);
            this.addChild(this._itemListPanel);
            this._pageBar = new PageSelecter();
            this._pageBar.mode = PageSelecter.SingleMode;
            this._pageBar.x = 100;
            this._pageBar.y = 307;
            this.addChild(this._pageBar);
            return;
        }

        public setDefaultPage(arg1: Array<any>, arg2: Boolean=false): void
        {
            this._isOpenByGRAPanel = arg2;
            if (arg1) 
            {
                this._pageTool.array = arg1;
            }
            this._pageBar.currentPage = 1;
            this._pageBar.maxPage = this._pageTool.pagesCount;
            this.updateItemListProvider(this._pageTool.getByPageNo(1));
            return;
        }

        public updateItemListProvider(arg1: Array<any>): void
        {
            var loc2=0;
            var loc1=new DataProvider();
            if (!(arg1 == null) && arg1.length > 0) 
            {
                loc2 = 0;
                while (loc2 < arg1.length) 
                {
                    loc1.addItem(arg1[loc2]);
                    ++loc2;
                }
                this._itemListPanel.dataProvider = loc1;
            }
            this._itemListPanel.dataProvider = loc1;
            return;
        }

        /* internal  */onCurPageChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=this._pageBar.currentPage;
            this.updateItemListProvider(this._pageTool.getByPageNo(loc1));
            return;
        }

        /* internal  */onOperateRequestInfoHandler(arg1: DataEvent): void
        {
            var loc4=null;
            var loc5=null;
            var loc1=arg1.data.applyInfo as SGuildWarehouseApply;
            var loc2=arg1.data.result;
            var loc3=arg1.data.moneyMap;
            if (loc2 && loc1 && loc1.itemCode > 0 && this._isOpenByGRAPanel) 
            {
                (loc4 = new SPlayerItem()).playerId = loc1.playerId;
                loc4.uid = loc1.uid;
                loc4.itemCode = loc1.itemCode;
                loc4.itemAmount = loc1.amount;
                loc4.jsStr = loc1.jsStr;
                loc5 = new ItemData(loc4);
                Dispatcher.dispatchEvent(new DataEvent(EventName.ApproveGuildWarehouseRecordsRequest, loc5));
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseReply, {"toPlayerId": loc1.playerId, "applyInfo": loc1, "result": loc2, "moneyMap": loc3}));
            }
            return;
        }

        public removeReplyedApply(arg1: SGuildWarehouseApply): void
        {
            this._itemListPanel.dataProvider.removeItem(arg1);
            this.removeItem(arg1);
            return;
        }

        /* internal  */removeItem(arg1: SGuildWarehouseApply): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._pageTool.array.length) 
            {
                loc1 = this._pageTool.array[loc2];
                if (loc1 && loc1 == arg1) 
                {
                    this._pageTool.array.splice(loc2, 1);
                }
                ++loc2;
            }
            this._pageTool.array = this._pageTool.array;
            this._pageBar.maxPage = this._pageTool.pagesCount;
            return;
        }

        /* internal */ /* var */_titleTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_itemListPanel: TilePanel;

        /* internal */ /* var */_pageBar: PageSelecter;

        /* internal */ /* var */_pageTool: PageTool;

        /* internal */ /* var */_isOpenByGRAPanel: Boolean;
    }
